Imports CrystalDecisions.Shared
Imports CrystalDecisions.CrystalReports.Engine

Partial Class Reportes_Controls_ctlCrReporteEntradaClub
    Inherits System.Web.UI.UserControl
    Protected Sub btnRefrescar_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim se As String = ""
        If Validar(se) = True Then
            BindReport()
        Else
            lblMensaje.ClientVisible = True
            lblMensaje.Text = se
        End If

    End Sub

    Protected Sub chkIncludeDateRange_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        If chkIncludeDateRange.Checked = True Then
            Me.txtStartDate.Enabled = True
            Me.txtEndDate.Enabled = True

        Else
            Me.txtStartDate.Enabled = False
            Me.txtEndDate.Enabled = False

        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim se As String = ""
        If Not IsPostBack Then
            chkIncludeDateRange.Checked = True
            Me.txtStartDate.Value = Now().Date
            Me.txtEndDate.Value = Now().Date


        End If
        Me.txtStartDate.MaxDate = DateTime.Today
        Me.txtStartDate.MinDate = "01/01/2011"
        Me.txtEndDate.MaxDate = DateTime.Today
        Me.txtEndDate.MinDate = "01/01/2011"

        'If Validar(se) = True Then
        BindReport()
        'Else
        'lblMensaje.ClientVisible = True
        'lblMensaje.Text = se
        'End If
        'BindReport()

    End Sub

    Private Sub BindReport()

        ''If Session("RepEntrada") Is Nothing Then
        Dim ConnInfo As New ConnectionInfo
        With ConnInfo
            .ServerName = DBUtilities.GetConnectionStringDataSource
            .DatabaseName = DBUtilities.GetConnectionStringDatabaseName
            .UserID = DBUtilities.GetConnectionStringUserID
            .Password = DBUtilities.GetConnectionStringPassWord
        End With

        Dim rep As New ReportDocument
        rep.Load(Server.MapPath("~/Reportes/arcReportes/rptReporteEntradaClub.rpt"))
        Me.CrystalReportViewer1.ReportSource = rep
        Dim RepTbls As Tables = rep.Database.Tables
        For Each RepTbl As Table In RepTbls
            Dim RepTblLogonInfo As TableLogOnInfo = RepTbl.LogOnInfo
            RepTblLogonInfo.ConnectionInfo = ConnInfo
            RepTbl.ApplyLogOnInfo(RepTblLogonInfo)
        Next
        Session("RepEntrada") = rep
        ''End If

        Me.CrystalReportViewer1.ReportSource = Session("RepEntrada")
        If Me.chkIncludeDateRange.Checked = True Then
            'Me.CrystalReportViewer1.SelectionFormula = "DateValue ({viewReporteEntradaClub.fechaIngreso})  >=  DateValue ('" + Format(txtStartDate.Value, "MM/dd/yyyy") + "') AND DateValue ({viewReporteEntradaClub.fechaIngreso})   <= DateValue ('" + Format(txtEndDate.Value, "MM/dd/yyyy") + "')"
            Me.CrystalReportViewer1.SelectionFormula = "DateValue ({viewReporteEntradaClub.fechaIngreso})  >=  DateValue (#" + Format(txtStartDate.Value, "MM/dd/yyyy") + "#) AND DateValue ({viewReporteEntradaClub.fechaIngreso})   <= DateValue (#" + Format(txtEndDate.Value, "MM/dd/yyyy") + "#)"

            ' Me.CrystalReportViewer1.SelectionFormula = "DateValue ({viewReporteEntradaClub.fechaIngreso})  >=  DateValue (" + txtStartDate.Value + ") AND DateValue ({viewReporteEntradaClub.fechaIngreso})   <= DateValue (" + txtEndDate.Value + ")"
        Else
            Me.CrystalReportViewer1.SelectionFormula = ""
        End If
        Me.CrystalReportViewer1.DataBind()
    End Sub

    Protected Sub txtStartDate_DateChanged(ByVal sender As Object, ByVal e As System.EventArgs)

    End Sub

    Protected Sub txtEndDate_DateChanged(ByVal sender As Object, ByVal e As System.EventArgs)

    End Sub

    Function Validar(ByRef sError As String) As Boolean
        Validar = False
        lblMensaje.ClientVisible = False
        If Me.txtStartDate.Value > Now() Then
            sError = "Fecha Inicial no puede ser mayor a la actual"
            Exit Function
        End If
        If Me.txtEndDate.Value > Now() Then
            sError = "Fecha Final no puede ser mayor a la actual"
            Exit Function
        End If
        If Me.txtEndDate.Value > Me.txtStartDate.Date Then
            sError = "Fecha Final no puede ser mayor a la Fecha inicial"
            Exit Function
        End If
        Validar = True
    End Function
End Class
